package jframe.utils;

import java.security.NoSuchAlgorithmException;
import java.security.Security;

import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;

import com.sun.crypto.provider.SunJCE;

/**
 * 功能描述：Des加密解密工具
 *
 * @author  叶平平(ypp)
 *
 * <p>修改历史：(修改人，修改时间，修改原因/内容)</p>
 */
public class TripleDesUtil {

	  private static final String Algorithm = "TripleDES";
	  private static final byte[] defaultKey = "poldk3921-@#swq9dwfaqa!s".getBytes();

	  static
	  {
	    Security.addProvider(new SunJCE());
	  }

	  private static byte[] encryptMode(byte[] keybyte, byte[] src)
	  {
	    try
	    {
	      SecretKey deskey = new SecretKeySpec(keybyte, "TripleDES");

	      Cipher c1 = Cipher.getInstance("TripleDES");
	      c1.init(1, deskey);
	      return c1.doFinal(src);
	    } catch (NoSuchAlgorithmException e1) {
	      e1.printStackTrace();
	    } catch (NoSuchPaddingException e2) {
	      e2.printStackTrace();
	    } catch (Exception e3) {
	      e3.printStackTrace();
	    }
	    return null;
	  }

	  private static byte[] decryptMode(byte[] keybyte, byte[] src)
	  {
	    try
	    {
	      SecretKey deskey = new SecretKeySpec(keybyte, "TripleDES");

	      Cipher c1 = Cipher.getInstance("TripleDES");
	      c1.init(2, deskey);
	      return c1.doFinal(src);
	    } catch (NoSuchAlgorithmException e1) {
	      e1.printStackTrace();
	    } catch (NoSuchPaddingException e2) {
	      e2.printStackTrace();
	    } catch (Exception e3) {
	      e3.printStackTrace();
	    }
	    return null;
	  }

	  private static byte uniteBytes(char c1, char c2)
	  {
	    return uniteBytes(String.valueOf(c1), String.valueOf(c2));
	  }

	  private static byte uniteBytes(String s1, String s2) {
	    byte b0 = Byte.decode("0x" + s1).byteValue();
	    b0 = (byte)(b0 << 4);
	    byte b1 = Byte.decode("0x" + s2).byteValue();
	    byte rebyte = (byte)(b0 | b1);
	    return rebyte;
	  }

	  public static String byte2hex(byte[] b)
	  {
	    String hs = "";
	    String stmp = "";
	    for (int n = 0; n < b.length; ++n) {
	      stmp = Integer.toHexString(b[n] & 0xFF);
	      if (stmp.length() == 1)
	        hs = hs + "0" + stmp;
	      else
	        hs = hs + stmp;
	    }
	    return hs.toUpperCase();
	  }

	  public static byte[] hex2bytes(String hex)
	  {
	    if (hex.length() % 2 != 0)
	      return null;

	    byte[] rebyte = new byte[hex.length() / 2];
	    for (int i = 0; i < rebyte.length; ++i)
	      rebyte[i] = uniteBytes(hex.charAt(i * 2), hex.charAt((i + 1) * 2 - 1));

	    return rebyte;
	  }

	  public static String encrypt(String src, String key)
	  {
	    String hexsec = null;
	    if (key != null) {
	      byte[] keys = key.getBytes();
	      byte[] secbyte = encryptMode(keys, src.getBytes());
	      hexsec = byte2hex(secbyte);
	    } else {
	      byte[] secbyte = encryptMode(defaultKey, src.getBytes());
	      hexsec = byte2hex(secbyte);
	    }
	    return hexsec;
	  }

	  public static String decrypt(String hexsec, String key)
	  {
	    byte[] sec = hex2bytes(hexsec);
	    String src = null;
	    if (key != null) {
	      byte[] keys = key.getBytes();
	      byte[] srcbyte = decryptMode(keys, sec);
	      src = new String(srcbyte);
	    } else {
	      byte[] srcbyte = decryptMode(defaultKey, sec);
	      src = new String(srcbyte);
	    }
	    return src;
	  }
	  public static void main(String[] args) {
		  TripleDesUtil des = new TripleDesUtil();
		  
		System.out.println(des.encrypt("123456", null));
		System.out.println(des.decrypt("0836889F0C5AECAF", "poldk3921-@#swq9dwfaqa!s"));
	}
}
